package com.lanhai.utils;

import java.util.Calendar;

/**
 * byte数组与16进制字符串的转换工具
 * 
 * @author liangmin
 *
 */
public class HexUtil {
	/**
	 * 判别A字符串是否包含字符串B
	 *
	 * @param message           A字符串
	 * @param detailbyte2HexStr 字符串B
	 * @return
	 */
	public static boolean contain(String message, String detail) {

		if (message.indexOf(detail) != -1) {
			return true;
		}
		return false;
	}

	/**
	 * crc16检测
	 *
	 * @param data
	 * @return
	 */
	private static int[] crcInt(int[] data) {
		int[] temdata = new int[data.length + 2];
		// unsigned char alen = *aStr 鈥?2; //CRC16鍙绠楀墠涓ら儴鍒?
		int xda, xdapoly;
		int i, j, xdabit;
		xda = 0xFFFF;
		xdapoly = 0xA001; // (X**16 + X**15 + X**2 + 1)
		for (i = 0; i < data.length; i++) {
			xda ^= data[i];
			for (j = 0; j < 8; j++) {
				xdabit = (int) (xda & 0x01);
				xda >>= 1;
				if (xdabit == 1)
					xda ^= xdapoly;
			}
		}
		System.arraycopy(data, 0, temdata, 0, data.length);
		temdata[temdata.length - 2] = (int) (xda & 0xFF);
		temdata[temdata.length - 1] = (int) (xda >> 8);
		return temdata;
	}

	/**
	 * 二次crc16检测
	 *
	 * @param data
	 * @return
	 */
	public static byte[] crc16(int[] data) {
		// 获取临时的crc数据
		int[] tempData = crcInt(data);
		// 定义发送的数据
		byte[] dataByte = new byte[tempData.length + 2];
		dataByte[0] = (byte) 0xA1;
		dataByte[dataByte.length - 1] = (byte) 0xB0;
		for (int i = 0; i < tempData.length; i++) {
			dataByte[i + 1] = (byte) tempData[i];
		}
		return dataByte;
	}

	public static void main(String[] args) {
		byte[] bArr = { -95, 1, 1, 0, 0, 0, 1, -95 };
		System.out.println(HexUtil.byte2HexStr(bArr));
	}

	/**
	 * hex转byte数组
	 * 
	 * @param hex
	 * @return
	 */
	public static byte[] hexToByte(String hex) {
		int m = 0, n = 0;
		int byteLen = hex.length() / 2; // 每两个字符描述一个字节
		byte[] ret = new byte[byteLen];
		for (int i = 0; i < byteLen; i++) {
			m = i * 2 + 1;
			n = m + 1;
			int intVal = Integer.decode("0x" + hex.substring(i * 2, m) + hex.substring(m, n));
			ret[i] = Byte.valueOf((byte) intVal);
		}
		return ret;
	}

	/**
	 * bytes转字符串字节转换为字符串样式
	 */
	public static String byte2HexStr(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = Integer.toHexString(b[n] & 0XFF);
			if (stmp.length() == 1)
				hs = hs + "0" + stmp;
			else
				hs = hs + stmp;
			// if (n<b.length-1) hs=hs+":";
		}
		return hs.toUpperCase();
	}

	/**
	 * bytes转字符串字节转换为字符串样式
	 */
	public static String byte2HexStr(byte[] b, int begin, int end) {
		String hs = "";
		String stmp = "";
		for (int n = begin; n < end; n++) {
			stmp = (Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1)
				hs = hs + "0" + stmp;
			else
				hs = hs + stmp;
			// if (n<b.length-1) hs=hs+":";
		}
		return hs.toUpperCase();
	}

	/**
	 * 检校和
	 *
	 * @param data
	 * @param len
	 * @return
	 */
	public static boolean checkAddDatas(byte[] data) {
		byte allCost = 0;
		for (int i = 0; i < data.length - 1; i++) {
			allCost += data[i];
		}

		// 不等处理
		if (allCost != data[data.length - 1]) {
			return false;
		}
		return true;
	}

	/**
	 * 獲取時間用於顯示
	 *
	 * @return
	 */
	public static String showTime() {
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
		int seconds = calendar.get(Calendar.SECOND);

		StringBuffer showTime = new StringBuffer();
		showTime.append(year).append("-").append(month).append("-").append(day);
		showTime.append("\t").append(hour).append(":").append(minute).append(":").append(seconds);

		return showTime.toString();
	}

	/**
	 * 显示日期
	 *
	 * @return
	 */
	public static String showDayTime() {
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DAY_OF_MONTH);

		StringBuffer showTime = new StringBuffer();
		showTime.append(year).append("-").append(month).append("-").append(day);

		return showTime.toString();
	}

	/**
	 *
	 * @return
	 */
	public static int getBanci() {
		Calendar c = Calendar.getInstance();

		System.out.println("原時間:" + c.getTimeInMillis());
		// 早班起始时间

		c.set(Calendar.HOUR_OF_DAY, 8);
		// c.set(Calendar.MINUTE, 30);
		// c.set(Calendar.SECOND, 00);
		long zanbanBegin = c.getTimeInMillis();
		System.out.println("早班开始:" + zanbanBegin);
		// 早班结束时间
		c.set(Calendar.HOUR_OF_DAY, 14);
		long zanbanEnd = c.getTimeInMillis();
		System.out.println("早班结束:" + zanbanEnd);
		return 0;
	}

	// byte[] 转 int 低字节在前（低字节序）
	public static int toIntL(byte[] b) {
		int res = 0;
		for (int i = 0; i < b.length; i++) {
			res += (b[i] & 0xff) << (i * 8);
		}
		return res;
	}

	// byte[] 转 int 高字节在前（高字节序）
	public static int toIntH(byte[] b) {
		int res = 0;
		for (int i = 0; i < b.length; i++) {
			res += (b[i] & 0xff) << ((3 - i) * 8);
		}
		return res;
	}

	/**
	 * 从指令数据中获取地标号
	 * 
	 * @param data
	 * @return
	 */
	public static int getGuidepost4Data(byte[] data) {
		/**
		 * 数据的第4位和第5位为地标号 data[3]:当前站点高8位, data[4]:当前站点底8位
		 */
		byte[] gpArr = { 0, 0, data[3], data[4] };
		int i = toIntH(gpArr);
		return i;
	}
}
